package ssm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ssm.dao.*;
import ssm.model.*;

import java.util.*;

@Service
public class WaysServiceImpl implements WaysService {


    @Autowired
    private TrackMapper trackMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private YktSfzhmMapper yktSfzhmMapper;
    @Autowired
    private WayMapper wayMapper;
    @Autowired
    private LocationCardMapper locationCardMapper;
    @Autowired
    private TrackCarMapper trackCarMapper;
//人员
    public Map<String, Object> getWay_person(String jid, String starttime, String endtime) {
        //查询轨迹点信息

        Long time=System.currentTimeMillis();
        Map<String,Object>result=new HashMap<String,Object>();
        List<Track>tracks=trackMapper.findTrack(jid,starttime,endtime);
        List<Map<String,Object>> ts=TrackTo(tracks);
        result.put("track",ts);
        System.out.println("1:"+(System.currentTimeMillis()-time));
        time=System.currentTimeMillis();
        //封装线路信息
        Map<String, Way> way_persons = new HashMap<String, Way>();
        Map<String,Object>way=new HashMap<String,Object>();
        String ways="";
        Integer startId=-1;
        Integer endId=-1;
        Way one=new Way();
        if(null!=tracks&&!tracks.isEmpty()){
            for(int i=tracks.size()-1;i>0;i--){
//                System.out.println(tracks.get(i));
//                System.out.println(tracks.get(i).getLocationid());
                if(tracks.get(i).getLocationid()!=null&&one!=null){
                    startId= tracks.get(i).getLocationid();
                }
                else if(startId == null || startId == -1){
                    startId= tracks.get(i).getLocationid();
                }
                if(tracks.get(i-1).getLocationid()!=null){
                    endId=tracks.get(i-1).getLocationid();
                }else{
                    continue;
                }
                if(startId==endId || startId == null || startId == -1){
                    continue;
                }
                one = way_persons.get(startId + "-" + endId);
                if(one != null) {
                    ways+=one.getWays();
                    continue;
                }
                else {
                    one = way_persons.get(endId + "-" + startId);
                    if(one != null) {
                        String [] strs=one.getWays().split(";");
                        for(int a=0;a<strs.length;a++){
                            ways+=strs[strs.length-1-a]+";";
                        }
                        continue;
                    }
                }
                one=wayMapper.getWay_person(startId,endId);
               // System.out.println(startId+"→"+endId+">>>>one:"+one);
                if(one!=null){
                    way_persons.put(startId + "-" + endId, one);
                    ways+=one.getWays();
                }else{
//                    System.out.println(1+"空");
                     one=wayMapper.getWay_person(endId,startId);
//                    System.out.println(endId+"→"+startId+">>>>two:"+one);
                    if(one!=null){
                        way_persons.put(endId + "-" + startId, one);
                        String [] strs=one.getWays().split(";");
                        for(int a=0;a<strs.length;a++){
                            ways+=strs[strs.length-1-a]+";";
                        }
                    }
                }
            }
            way.put("ways",ways);
            way.put("startTime",tracks.get(tracks.size()-1).getSj());
            way.put("endTime",tracks.get(0).getSj());
            way.put("startCamerName",tracks.get(tracks.size()-1).getDd());
            way.put("endCamerName",tracks.get(0).getDd());
        }

        result.put("ways",way);
        System.out.println("2:"+(System.currentTimeMillis()-time));
        time=System.currentTimeMillis();
        //返回结果
        return result;
    }

    private List<Map<String,Object>> TrackTo(List<Track> tracks) {
        List<Map<String,Object>>result=new ArrayList<Map<String,Object>>();
        for(Track t:tracks){
            Map<String,Object>map=new HashMap<String,Object>();
            map.put("id",t.getId());
            map.put("xm",t.getXm());
            map.put("jid",t.getXhgh());
            map.put("passTime",t.getSj());
            map.put("position",t.getDd());
            map.put("lx",t.getLx());
            map.put("lsh",t.getLsh());
            map.put("sbbh",t.getSbbh());
            map.put("zpzp",t.getZpzp());
            map.put("xsd",t.getXsd());
            map.put("camerJd",t.getDevicejd());
            map.put("camerWd",t.getDevicewd());
            map.put("ly",t.getLy());
            map.put("locationid",t.getLocationid());
            map.put("deviceid",t.getDeviceid());
            result.add(map);
        }
        return result;
    }

    public Map<String, Object> getWay_car(String cph, String starttime, String endtime) {
        Map<String,Object>result=new HashMap<String,Object>();
        //封装轨迹点位
        Map<String,Object>param=new HashMap<String,Object>();
        param.put("cph",cph);
        param.put("starttime",starttime);
        param.put("endtime",endtime);
        List<TrackCar>trackCars=trackCarMapper.getOneByTime(param);
        result.put("track",trackCars);
        //封装线路信息
        Map<String, Way> way_persons = new HashMap<String, Way>();
        Map<String,Object>way=new HashMap<String,Object>();
        String ways="";
        Integer startId=-1;
        Integer endId=-1;
        Way one=new Way();
        if(null!=trackCars&&!trackCars.isEmpty()){
            for(int i=trackCars.size()-1;i>0;i--){
//                System.out.println(tracks.get(i));
//                System.out.println(tracks.get(i).getLocationid());
                if(trackCars.get(i).getLocationid()!=null&&one!=null){
                    startId= trackCars.get(i).getLocationid();
                }
                else if(startId == null || startId == -1){
                    startId= trackCars.get(i).getLocationid();
                }
                if(trackCars.get(i-1).getLocationid()!=null){
                    endId=trackCars.get(i-1).getLocationid();
                }else{
                    continue;
                }
                if(startId==endId || startId == null || startId == -1){
                    continue;
                }
                one = way_persons.get(startId + "-" + endId);
                if(one != null) {
                    ways+=one.getWays();
                    continue;
                }
                else {
                    one = way_persons.get(endId + "-" + startId);
                    if(one != null) {
                        String [] strs=one.getWays().split(";");
                        for(int a=0;a<strs.length;a++){
                            ways+=strs[strs.length-1-a]+";";
                        }
                        continue;
                    }
                }
                one=wayMapper.getWay_person(startId,endId);
                // System.out.println(startId+"→"+endId+">>>>one:"+one);
                if(one!=null){
                    way_persons.put(startId + "-" + endId, one);
                    ways+=one.getWays();
                }else{
//                    System.out.println(1+"空");
                    one=wayMapper.getWay_person(endId,startId);
//                    System.out.println(endId+"→"+startId+">>>>two:"+one);
                    if(one!=null){
                        way_persons.put(endId + "-" + startId, one);
                        String [] strs=one.getWays().split(";");
                        for(int a=0;a<strs.length;a++){
                            ways+=strs[strs.length-1-a]+";";
                        }
                    }
                }
            }
            way.put("ways",ways);
            way.put("startTime",trackCars.get(trackCars.size()-1).getJgsj());
            way.put("endTime",trackCars.get(0).getJgsj());
            way.put("startCamerName",trackCars.get(trackCars.size()-1).getKkmc());
            way.put("endCamerName",trackCars.get(0).getKkmc());
        }

        result.put("ways",way);
        //返回结果
        return result;
    }



    private Map<String, Object> wayToMap(Way way, String starTime, String endTime) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (way != null) {
            map.put("camer1Name", way.getCamer1Name());
            map.put("camer2Name", way.getCamer2Name());
            map.put("ways", way.getWays());
        }
        map.put("camer1Time", starTime);
        map.put("camer2Time", endTime);
        return map;
    }

    private PersonBase studentToPreson(Student student) {
        PersonBase person = new PersonBase();
        person.setJid(student.getXh());
        person.setXm(student.getXm());
        person.setXb(student.getXb());
        person.setCsrq(student.getCsrq());
        person.setJg(student.getJg());
        person.setSfzjh(student.getSfzjh());
        person.setSjhm(student.getSjhm());
        person.setZz(student.getZz());
        person.setZp(student.getZp());
        person.setMz(student.getMz());
        person.setZzmm(student.getMz());
        return person;
    }

    private PersonBase teacherToPreson(Teacher teacher) {
        PersonBase person = new PersonBase();
        person.setJid(teacher.getGh());
        person.setXm(teacher.getXm());
        person.setXb(teacher.getXb());
        person.setCsrq(teacher.getCsrq());
        person.setJg(teacher.getJg());
        person.setSfzjh(teacher.getSfzjh());
        person.setSjhm(teacher.getSjhm());
        person.setZz(teacher.getZz());
        person.setZp(teacher.getZp());
        person.setMz(teacher.getMz());
        person.setZzmm(teacher.getMz());
        return person;
    }



    public List<Map<String, Object>> toMap(List<TrackCar> cars) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (TrackCar trackCar : cars) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("carPlate", trackCar.getCphm());
            map.put("passTime", trackCar.getJgsj());
            map.put("plateColor", trackCar.getCpys());
            map.put("direction", trackCar.getCdfx());
            map.put("sbbh", trackCar.getSbbh());
            map.put("zyb", trackCar.getZyb());
            map.put("Kkmc", trackCar.getKkmc());
            map.put("clpp", trackCar.getClpp());

            map.put("postion", trackCar.getJgdd());
            map.put("xszt", trackCar.getXszt());
            map.put("cs", trackCar.getCs());
            map.put("cplx", trackCar.getCplx());

            map.put("zpp", trackCar.getZpp());
            map.put("cllx", trackCar.getCllx());
            map.put("zp", trackCar.getZp());
            map.put("belong", trackCar.getBelong());
            map.put("Clys", trackCar.getClys());
            map.put("camerJd", trackCar.getCamerjd());
            map.put("camerWd", trackCar.getCamerwd());
            // map.put("barrName",trackCar.getJgdd());
            list.add(map);
        }
        return list;
    }


}
